<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <title>tiptap Link 超链接 - itxst.com</title>
    <meta name="keywords" content="tiptap Link 超链接" />
    <meta name="description" content="tiptap Link 扩展支持在文档中创建超链接，tiptap没有提供UI界面，添加删除编辑 URL 链接你需要通过tiptap命令来执行，粘贴的url将自动转换为链接。" />
    <meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
    <meta http-equiv="Content-Language" content="zh-CN">
    <link rel="shortcut icon" href="/img/favicon.ico" type="image/x-icon" />
    <link href="/css/zt.css?ver=1.6.6" rel="stylesheet" type="text/css" media="all" />
    <script type="text/javascript" language="javascript" src="/js/jquery.js?ver=1.6.6"></script>
    <script type="text/javascript" src="/js/highlight/highlight.min.js?ver=1.6.6"></script>
    <link rel="stylesheet" type="text/css" href="/js/highlight/styles/vs2015x.css?ver=1.6.6">
    <script type="text/javascript" language="javascript" src="/js/comm.js?ver=1.6.6"></script>
    <script type="text/javascript" language="javascript" src="/js/zt.js?ver=1.6.6"></script>
    <script type="text/javascript" charset="utf-8" src="/js/layer/layer.js?ver=1.6.6"></script>
    <link rel="alternate" media="only screen and(max-width: 640px)" href="http://m.itxst.com/tiptap/ijiajfnm.html" >
    <!--[if lte IE 9]>
    <script src="/js/respond/respond.js?ver=1.6.6"></script>
    <![endif]-->
</head>
<body>
    <div class="body"  >
        <div class="header">
            <div class="naver">
                <div class="logo">
                    <a href="/"><img src="/img/logov31.png" /></a>
                </div>
                <div class="naver-left">
                 <a href="/tiptap/tutorial.html">tiptap 中文文档</a>
                </div>
                <div class="naver-search">
                    <div class="naver-search-txt">
                        <input type="text" id="keyword" placeholder="Please enter the keyword" data-topic="tiptap">
                    </div>
                    <div class="naver-search-btn"><a href="javascript:void(0)" onclick="Search()">Search</a></div>
                </div>
                 <div class="naver-right"> 
                        <a href="/ag-grid/tutorial.html">AG-GRID</a> 
                        <a href="/tiptap/tutorial.html">Tiptap</a> 
                        <a href="/css-flex/tutorial.html">CSS-FLEX</a> 
                        <a href="/Bootstrap-Table/QuickStart.html">Bootstrap-Table</a> 
                </div>
            </div>
            <div class="path">
                
            </div>
        </div>
        <div class="clearfix"></div>
        <div class="pagew">
        <div class="page" >
            <div class="page-body">
                <div class="content-detail">
                    <h1>tiptap Link 超链接</h1>
               
               <p class="p">tiptap <b>Link</b> 扩展支持在文档中创建超链接，tiptap 没有提供 <b>UI</b>界面，添加删除编辑 URL 链接你需要通过tiptap命令来执行，粘贴的url将自动转换为链接。</p><h2>Install 安装</h2><pre class="brush:js;toolbar:false"><code class="language-javascript">npm install @tiptap/extension-link</code></pre><h2>Settings 配置</h2><p class="pe"><b>protocols</b> 自定义可以识别为链接的协议。</p><pre class="brush:js;toolbar:false"><code class="language-javascript">Link.configure({
  protocols: ['ftp', 'mailto']
})</code></pre><p class="pe"><b>autolink</b>&nbsp;当用户在编辑器内输入网址时自动加上超链接。</p><pre class="brush:js;toolbar:false"><code class="language-javascript">Link.configure({
  autolink: false
})</code></pre><p class="pe"><b>openOnClick</b> 点击时打开链接。</p><pre class="brush:js;toolbar:false"><code class="language-javascript">Link.configure({
  openOnClick: false
})</code></pre><p class="pe"><b>linkOnPaste</b> 粘贴内容时是否将内容里面的url转换为链接。</p><pre class="brush:js;toolbar:false"><code class="language-javascript">Link.configure({
  linkOnPaste: false
})</code></pre><p class="pe"><b>HTMLAttributes</b> 自定义标签属性。</p><pre class="brush:js;toolbar:false"><code class="language-javascript">Link.configure({
  HTMLAttributes: {
    class: 'my-custom-class',
    rel: 'noopener noreferrer',
    // Remove target entirely so links open in current tab
    target: null,
  },
})</code></pre><p class="pe"><b>validate</b>&nbsp;自动识别创建链接时对URL进行校验，比如你可以用来只能创建<a href="https://www.itxst.com" _src="https://www.itxst.com">https://www.itxst.com</a>&nbsp;的链接 。</p><pre class="brush:js;toolbar:false"><code class="language-javascript">// only autolink urls with a protocol
Link.configure({
  validate: href =&gt; /^https?:\/\//.test(href),
})</code></pre><h2>Commands 命令</h2><p class="pe"><b>setLink</b>&nbsp;创建超链接。</p><pre class="brush:js;toolbar:false"><code class="language-javascript">editor.commands.setLink({ href: 'https://www.itxst.com' })
editor.commands.setLink({ href: 'https://www.itxst.com', target: '_blank' })</code></pre><p class="pe"><b>ttoggleLink</b>&nbsp;切换为超链接。</p><pre class="brush:js;toolbar:false"><code class="language-javascript">editor.commands.toggleLink()</code></pre><p class="pe"><b>unsetLink</b>&nbsp;移除链接。</p><pre class="brush:js;toolbar:false"><code class="language-javascript">editor.commands.unsetLink()</code></pre><p class="pe"><b>获取当前链接</b></p><pre class="brush:js;toolbar:false"><code class="language-javascript">this.editor.getAttributes('link').href</code></pre><h2>源代码</h2><p><a href="https://github.com/ueberdosis/tiptap/blob/main/packages/extension-link/" target="_blank" textvalue="link 源代码">link 源代码</a></p><h2>在线例子</h2><p><a href="https://debug.itxst.com/static/tiptapdemo/index.html#link" target="_blank" textvalue="vue 在线例子">vue 在线例子</a></p><h2>Link 例子</h2><div class="mcode"><ul class=" list-paddingleft-2"><li><p class="mcode-curr">Vue 例子</p></li><li><p>React 例子</p></li><li><p>React CSS</p></li></ul><pre class="brush:html;toolbar:false"><code class="language-html">&lt;template&gt;
  &lt;div v-if="editor"&gt;
    &lt;button @click="setLink" :class="{ 'is-active': editor.isActive('link') }"&gt;
      setLink
    &lt;/button&gt;
    &lt;button @click="editor.chain().focus().unsetLink().run()" :disabled="!editor.isActive('link')"&gt;
      unsetLink
    &lt;/button&gt;
    &lt;editor-content :editor="editor" /&gt;
  &lt;/div&gt;
&lt;/template&gt;
&lt;script&gt;
import Code from '@tiptap/extension-code'
import Document from '@tiptap/extension-document'
import Link from '@tiptap/extension-link'
import Paragraph from '@tiptap/extension-paragraph'
import Text from '@tiptap/extension-text'
import { Editor, EditorContent } from '@tiptap/vue-3'

export default {
  components: {
    EditorContent,
  },
  data() {
    return {
      editor: null,
    }
  },
  mounted() {
    this.editor = new Editor({
      extensions: [
        Document,
        Paragraph,
        Text,
        Code,
        Link.configure({
          openOnClick: false,
        }),
      ],
      content: `
        &lt;p&gt;
          Wow, this editor has support for links to the whole &lt;a href="https://en.wikipedia.org/wiki/World_Wide_Web"&gt;world wide web&lt;/a&gt;. We tested a lot of URLs and I think you can add *every URL* you want. Isn’t that cool? Let’s try &lt;a href="https://statamic.com/"&gt;another one!&lt;/a&gt; Yep, seems to work.
        &lt;/p&gt;
        &lt;p&gt;
          By default every link will get a &lt;code&gt;rel="noopener noreferrer nofollow"&lt;/code&gt; attribute. It’s configurable though.
        &lt;/p&gt;
      `,
    })
  },
  methods: {
    setLink() {
      const previousUrl = this.editor.getAttributes('link').href
      const url = window.prompt('URL', previousUrl)
      // cancelled
      if (url === null) {
        return
      }
      // empty
      if (url === '') {
        this.editor
          .chain()
          .focus()
          .extendMarkRange('link')
          .unsetLink()
          .run()
        return
      }
      // update link
      this.editor
        .chain()
        .focus()
        .extendMarkRange('link')
        .setLink({ href: url })
        .run()
    },
  },
  beforeUnmount() {
    this.editor.destroy()
  },
}
&lt;/script&gt;
&lt;style&gt;
/* Basic editor styles */
.ProseMirror {
  &gt; * + * {
    margin-top: 0.75em;
  }
  a {
    color: #68CEF8;
  }
  code {
    font-size: 0.9rem;
    padding: 0.25em;
    border-radius: 0.25em;
    background-color: rgba(#616161, 0.1);
    color: #616161;
    box-decoration-break: clone;
  }
}
&lt;/style&gt;</code></pre><pre class="brush:js;toolbar:false"><code class="language-javascript">import './styles.scss'
import Code from '@tiptap/extension-code'
import Document from '@tiptap/extension-document'
import Link from '@tiptap/extension-link'
import Paragraph from '@tiptap/extension-paragraph'
import Text from '@tiptap/extension-text'
import { EditorContent, useEditor } from '@tiptap/react'
import React, { useCallback } from 'react'

export default () =&gt; {
  const editor = useEditor({
    extensions: [
      Document,
      Paragraph,
      Text,
      Code,
      Link.configure({
        openOnClick: false,
      }),
    ],
    content: `
        &lt;p&gt;
          Wow, this editor has support for links to the whole &lt;a href="https://en.wikipedia.org/wiki/World_Wide_Web"&gt;world wide web&lt;/a&gt;. We tested a lot of URLs and I think you can add *every URL* you want. Isn’t that cool? Let’s try &lt;a href="https://statamic.com/"&gt;another one!&lt;/a&gt; Yep, seems to work.
        &lt;/p&gt;
        &lt;p&gt;
          By default every link will get a &lt;code&gt;rel="noopener noreferrer nofollow"&lt;/code&gt; attribute. It’s configurable though.
        &lt;/p&gt;
      `,
  })

  const setLink = useCallback(() =&gt; {
    const previousUrl = editor.getAttributes('link').href
    const url = window.prompt('URL', previousUrl)
    // cancelled
    if (url === null) {
      return
    }
    // empty
    if (url === '') {
      editor.chain().focus().extendMarkRange('link').unsetLink()
        .run()
      return
    }
    // update link
    editor.chain().focus().extendMarkRange('link').setLink({ href: url })
      .run()
  }, [editor])
  if (!editor) {
    return null
  }
  return (
    &lt;&gt;
      &lt;button onClick={setLink} className={editor.isActive('link') ? 'is-active' : ''}&gt;
        setLink
      &lt;/button&gt;
      &lt;button
        onClick={() =&gt; editor.chain().focus().unsetLink().run()}
        disabled={!editor.isActive('link')}
      &gt;
        unsetLink
      &lt;/button&gt;
      &lt;EditorContent editor={editor} /&gt;
    &lt;/&gt;
  )
}</code></pre><pre class="brush:js;toolbar:false"><code class="language-javascript">/* Basic editor styles */
.ProseMirror {
  &gt; * + * {
    margin-top: 0.75em;
  }
  a {
    color: #68cef8;
  }
  code {
    background-color: rgba(#616161, 0.1);
    border-radius: 0.25em;
    box-decoration-break: clone;
    color: #616161;
    font-size: 0.9rem;
    padding: 0.25em;
  }
}</code></pre></div>
 
               <h2>
    下载教程 Demo
</h2>
<p class="pe">
   本教程<b>Demo</b>的源码点击这里下载，<a href="https://img1.itxst.com/8/c/8c32fcdc12cb8847c62e405be8a73835.zip" target="_blank" textvalue="Tiptap Demo">Tiptap Demo</a>，下载完成后运行<b>npm i</b>初始化依赖包。
</p>
                </div>
                <div class="content-r">   <a href="/tiptap/tutorial.html" target="_self">
                     <img src="https://img1.itxst.com/0/9/09355b3592a0c16799b504a9f2a70303.png" alt="tiptap 中文文档" />
                    </a>                                                <a href="//www.itxst.com/vue-draggable/tutorial.html" target="_blank">
                                <img src="//img1.itxst.com/b/f/bf9af8368dfac84cd703ef54e1e89d14.png" alt="vue.draggable" />
                            </a>
 
                </div>
            </div>
            <div class="home-menu scroll">
                <div class="tree">
      <a data-level="1" class="leve-1   doc-1   " href="/tiptap/tutorial.html" title="tiptap 中文文档">快速入门</a>
      <a data-level="1" class="leve-1    arrow-1" href="/tiptap/configuration.html" title="Guide 向导">Guide 向导</a>
      <a data-level="1" class="leve-1    arrow-2" href="/tiptap/api.html" title="tiptap api 列表">API 列表</a>
      <a data-level="2" class="leve-2   doc-1   " href="/tiptap/methods.html" title="tiptap editor 方法">tiptap 方法</a>
      <a data-level="2" class="leve-2   doc-1   " href="/tiptap/attrs.html" title="tiptap 属性">tiptap 属性</a>
      <a data-level="2" class="leve-2   doc-1   " href="/tiptap/settings.html" title="titap 配置">titap 配置</a>
      <a data-level="2" class="leve-2    arrow-1" href="/tiptap/uaqmeemr.html" title="tiptap commands 命令">commands 命令</a>
      <a data-level="2" class="leve-2    arrow-1" href="/tiptap/363qzvqv.html" title="nodes 节点">nodes 节点</a>
      <a data-level="2" class="leve-2    arrow-2" href="/tiptap/marks.html" title="tiptap marks 标记">marks 标记</a>
      <a data-level="3" class="leve-3   doc-1   " href="/tiptap/2m6bzqij.html" title="tiptap Bold 粗体">Bold 粗体</a>
      <a data-level="3" class="leve-3   doc-1   " href="/tiptap/j2yzin77.html" title="tiptap Code 标签">Code 标签</a>
      <a data-level="3" class="leve-3   doc-1   " href="/tiptap/mfezuej2.html" title="tiptap Highlight 高亮标记">Highlight 高亮标记</a>
      <a data-level="3" class="leve-3   doc-1   " href="/tiptap/6fquiarf.html" title="tiptap Italic 斜体">Italic 斜体</a>
      <a data-level="3" class="leve-3   doc-1    doc-2 " href="/tiptap/ijiajfnm.html" title="tiptap Link 超链接">Link 超链接</a>
      <a data-level="3" class="leve-3   doc-1   " href="/tiptap/6vfiiunz.html" title="tiptap Strike 删除符">Strike 删除符</a>
      <a data-level="3" class="leve-3   doc-1   " href="/tiptap/nyznjqfn.html" title="tiptap Subscript 下标">Subscript 下标</a>
      <a data-level="3" class="leve-3   doc-1   " href="/tiptap/mrzbqrii.html" title="tiptap TextStyle 文本样式">TextStyle 文本样式</a>
      <a data-level="3" class="leve-3   doc-1   " href="/tiptap/arumim7r.html" title="tiptap Underline 下划线">Underline 下划线</a>
      <a data-level="3" class="leve-3   doc-1   " href="/tiptap/ufjeeqrm.html" title="tiptap Superscript 上标">Superscript 上标</a>
      <a data-level="2" class="leve-2    arrow-1" href="/tiptap/extensions.html" title="tiptap extensions 扩展">extensions 扩展</a>
                 </div>
                 <div  class="clearfix"></div>
            </div>
        </div>
        <div class="clearfix"></div>
           <div class="footer">
© 2023 itxst.com All rights reserved.
</div>

<script>
var _hmt = _hmt || [];
(function() {
  var hm = document.createElement("script");
  hm.src = "https://hm.baidu.com/hm.js?7e15ed96eb8133218e99de8bc1f5cb61";
  var s = document.getElementsByTagName("script")[0];
  s.parentNode.insertBefore(hm, s);
})();
</script>
       </div>
    </div> 
</body>
</html>
